<?php


/**
 * Function Library for HTML Form Elements
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @package core
 * @version 2
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * returns a well formed input tag
 *
 * @param      array    	array with attributes for input tag
 * @param      bool    		mark as needed (default: false)
 * @param      bool    		mark it anyway (default: false)
 * @return     string		input tag
 */
function FormInput($array = array (), $required = false, $mark = false) {
	if (!is_array($array))
		return false;
	$origin_array = array (
		"CLASS" => "form",
		"NAME" => "unnamed",
		"TYPE" => "TEXT",
		"SIZE" => "20",
		"MAXLENGTH" => "255",
		"VALUE" => ""
	);
	# uppercase
	foreach ($array as $k => $v) {
		unset ($array[$k]);
		$array[strtoupper($k)] = $v;
	}
	$tag = "<input ";
	$result = array_merge($origin_array, $array);
	switch (strtoupper($result['TYPE'])) {
		case "HIDDEN" :
			unset ($result['CLASS'], $result['SIZE'], $result['MAXLENGTH']);
			break;
		case "SUBMIT" :
			unset ($result['SIZE'], $result['MAXLENGTH']);
			if ($result['CLASS'] == "form")
				$result['CLASS'] = "formbutton";
			$result['OnClick'] = "_body_onunload(); this.form.submit();";
			$result['TYPE'] = "button";
			break;
		case "RADIO" :
			$result['CLASS'] = "formradio";
			break;
	}
	foreach ($result as $k => $v) {
		if ($v == "")
			continue;
		if (strtoupper($k) == "VALUE" AND !preg_match("=^(submit)$=si", $result["TYPE"]))
			$tag .= strtoupper($k) . '="' . HtmlFormat(stripslashes($v)) . '" ';
		else
			$tag .= strtoupper($k) . '="' . $v . '" ';
	}
	# if no value given try to autoset
	if (sendvar($result['NAME']) && $result['VALUE'] == "")
		$tag .= 'VALUE="' . HtmlFormat(stripslashes(sendvar($result['NAME']))) . '" ';
	if (($required && !sendvar($result['NAME'])) || $mark)
		$tag = REQUIREDGRAPH . $tag;
	return $tag . "/>";
}
/**
* returns a well formed textarea tag
*
* @param      array    	array with attributes for textarea tag
* @param      bool    	mark as needed (default: false)
* @param      bool    	mark it anyway (default: false)
* @return     string	input tag
*/
function FormTextarea($array = array (), $required = false, $mark = false) {
	if (!is_array($array))
		return false;
	$origin_array = array (
		"CLASS" => "form",
		"NAME" => "unnamed",
		"COLS" => "80",
		"ROWS" => "8"
	);
	# uppercase
	foreach ($array as $k => $v) {
		unset ($array[$k]);
		$array[strtoupper($k)] = $v;
	}
	$result = array_merge($origin_array, $array);
	# extract value and remove from entity
	if (isset ($result['VALUE'])) {
		sendvar_set($result['NAME'], $result['VALUE']);
		unset ($result['VALUE']);
	}
	$tag = "<TEXTAREA ";
	foreach ($result as $k => $v) {
		if ($v == "")
			continue;
		$tag .= strtoupper($k) . '="' . $v . '" ';
	}
	if (($required && !sendvar($result['NAME'])) || $mark)
		$tag = REQUIREDGRAPH . $tag;
	$tag .= '>' . HtmlFormat(stripslashes(sendvar($result['NAME'])));
	return $tag . "</TEXTAREA>";
}
/**
* returns a well formed checkbox
*
* @param      array    	array with attributes for textarea tag
* @param      bool    	mark as needed (default: false)
* @param      bool    	mark it anyway (default: false)
* @return     string	input tag
*/
function FormCheckbox($array = array (), $required = false, $mark = false) {
	if (!is_array($array))
		return false;
	$origin_array = array (
		"CLASS" => "formcheck",
		"NAME" => "unnamed",
		"VALUE" => "",
		"TYPE" => "checkbox",
		"CHECKED" => ""
	);
	# uppercase
	foreach ($array as $k => $v) {
		unset ($array[$k]);
		$array[strtoupper($k)] = $v;
	}
	$tag = "<INPUT ";
	$result = array_merge($origin_array, $array);
	foreach ($result as $k => $v) {
		if ($v == "")
			continue;
		if (strtoupper($k) == "VALUE")
			$tag .= strtoupper($k) . '="' . HtmlFormat(stripslashes($v)) . '" ';
		else
			$tag .= strtoupper($k) . '="' . $v . '" ';
	}
	# if no value given try to autoset
	if (sendvar($result['NAME']) && $result['VALUE'] == "")
		$tag .= 'checked="checked" ';
	if (($required && !sendvar($result['NAME'])) || $mark)
		$tag = REQUIREDGRAPH . $tag;
	return $tag . "/>";
}
/**
 * returns a well formed select tag
 *
 * @param      array    array with attributes for select tag
 * @param      array   	assoc.array with values and text e.g. array("1" => "any text")
 * @param      array   	flat array with values that be preselcted e.g. select value 1 = array("1")
 * @param      bool    	mark it anyway (default: false)
 *
 * @return     string	select tag
 */
function FormSelect($array = array (), $rows = array (), $preselect = array (), $mark = false) {
	if (!is_array($array))
		return false;
	$origin_array = array (
		"CLASS" => "formselect",
		"NAME" => "unnamed",
		"SIZE" => "5"
	);
	# uppercase
	foreach ($array as $k => $v) {
		unset ($array[$k]);
		$array[strtoupper($k)] = $v;
	}
	// make array of preselect
	if (!is_array($preselect))
		$preselect = array ($preselect);
	$result = array_merge($origin_array, $array);
	# check if multiple
	if (!isset ($result['MULTIPLE']) && !isset ($array['SIZE']))
		unset ($result['SIZE']);
	$tag = '<SELECT ';
	foreach ($result as $k => $v) {
		if ($v == "")
			continue;
		$tag .= strtoupper($k) . '="' . $v . '" ';
	}
	$tag .= ">";
	# place values and text
	foreach ($rows as $k => $v) {
		$tag .= '<OPTION VALUE="' . $k . '"';
		if (in_array($k,$preselect))
			$tag .= ' selected="selected"';
		$tag .= '>' . ($v) . '</OPTION>';
	}
	if ($mark)
		$tag = REQUIREDGRAPH . $tag;
	return $tag . "</SELECT>";
}
?>